package cn.iocoder.yudao.module.erp.controller.admin.payinfo;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
import cn.iocoder.yudao.module.erp.controller.admin.contractinfo.vo.ContractInfoRespVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.contractinfo.ContractInfoDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.customerinfo.CustomerInfoDO;
import cn.iocoder.yudao.module.erp.service.customerinfo.CustomerInfoService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;
import java.util.stream.Collectors;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;

import cn.iocoder.yudao.module.erp.controller.admin.payinfo.vo.*;
import cn.iocoder.yudao.module.erp.dal.dataobject.payinfo.PayInfoDO;
import cn.iocoder.yudao.module.erp.service.payinfo.PayInfoService;

@Tag(name = "管理后台 - 付款记录信息")
@RestController
@RequestMapping("/erp/pay-info")
@Validated
public class PayInfoController {

    @Resource
    private PayInfoService payInfoService;

    @Resource
    private CustomerInfoService customerInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建付款记录信息")
    @PreAuthorize("@ss.hasPermission('erp:pay-info:create')")
    public CommonResult<Long> createPayInfo(@Valid @RequestBody PayInfoSaveReqVO createReqVO) {
        return success(payInfoService.createPayInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新付款记录信息")
    @PreAuthorize("@ss.hasPermission('erp:pay-info:update')")
    public CommonResult<Boolean> updatePayInfo(@Valid @RequestBody PayInfoSaveReqVO updateReqVO) {
        payInfoService.updatePayInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除付款记录信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:pay-info:delete')")
    public CommonResult<Boolean> deletePayInfo(@RequestParam("id") Long id) {
        payInfoService.deletePayInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得付款记录信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:pay-info:query')")
    public CommonResult<PayInfoRespVO> getPayInfo(@RequestParam("id") Long id) {
        PayInfoDO payInfo = payInfoService.getPayInfo(id);

        //得到客户



        return success(BeanUtils.toBean(payInfo, PayInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得付款记录信息分页")
    @PreAuthorize("@ss.hasPermission('erp:pay-info:query')")
    public CommonResult<PageResult<PayInfoRespVO>> getPayInfoPage(@Valid PayInfoPageReqVO pageReqVO) {
        PageResult<PayInfoDO> pageResult = payInfoService.getPayInfoPage(pageReqVO);

        if (CollUtil.isEmpty(pageResult.getList())) {
            return success(PageResult.empty(pageResult.getTotal()));
        }
        // 客户信息
        List<CustomerInfoDO> customerList = customerInfoService.getCustomerListIds(
                convertSet(pageResult.getList(), PayInfoDO::getCustomerId));
        Map<Long,CustomerInfoDO> customerMap=customerList.stream().collect(Collectors.toMap(CustomerInfoDO::getId, e -> e));




        //return success(BeanUtils.toBean(pageResult, ContractInfoRespVO.class));
        return success(BeanUtils.toBean(pageResult, PayInfoRespVO.class, purchaseIn -> {
            MapUtils.findAndThen(customerMap, purchaseIn.getCustomerId(), cus -> purchaseIn.setCustomerName(cus.getName()));
        }));

       // return success(BeanUtils.toBean(pageResult, PayInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出付款记录信息 Excel")
    @PreAuthorize("@ss.hasPermission('erp:pay-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPayInfoExcel(@Valid PayInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PayInfoDO> list = payInfoService.getPayInfoPage(pageReqVO).getList();

        // 客户信息
        List<CustomerInfoDO> customerList = customerInfoService.getCustomerListIds(
                convertSet(list, PayInfoDO::getCustomerId));
        Map<Long,CustomerInfoDO> customerMap=customerList.stream().collect(Collectors.toMap(CustomerInfoDO::getId, e -> e));

        // 导出 Excel
        ExcelUtils.write(response, "付款记录信息.xls", "数据", PayInfoRespVO.class,
                        BeanUtils.toBean(list, PayInfoRespVO.class,plan->{
                            MapUtils.findAndThen(customerMap, plan.getCustomerId(), cus -> plan.setCustomerName(cus.getName()));
                        }));
    }

}